apiready = function() {
    seajs.use(["../../api", "../../jquery", "../../app", "../../service", "../../dialog"], function($api, $, $app, service, dialog) {
        var vm = {
            el: '.user_edit_box',
            data: {
                userInfo: {},
                Version: "0.0.0",
                isPushStopped_num: 0,
                openId: ''
            },
            mounted: function() {
                var that = this;
                weiXin = api.require('wx');
                ajpush = api.require('ajpush');
                api.addEventListener({
                    name: 'U_up'
                }, function(ret, err) {
                    vue.U_up()
                })

                this.init();
            },
            methods: {
                // 初始化
                init: function() {
                    var that = this
                    this.isPushStopped(function(num) {
                        that.isPushStopped_num = num;
                        that.userInfo = $api.getStorage('user').data
                        console.log(JSON.stringify(that.userInfo));
                        that.Version = api.appVersion + "(" + $app.appVersion + ")"
                    })

                    //通过unionid获取openid
                    $app.ajax(
                        service['getOpenId'], {
                            data: {
                                values: {
                                    'unionid': $api.getStorage("user").shop_unionid
                                }
                            },
                            method: 'post',
                            showMask: true
                        },
                        function(res) {
                            // alert(JSON.stringify(res));
                            that.openId = res.data;
                        }
                    )
                },
                // 查看极光推送通知是否开启
                isPushStopped: function(cb) {
                    ajpush.isPushStopped(function(ret) {
                        cb(ret.isStopped)
                    })
                },
                // 更换头像
                upHeaderImage: function() {
                    var that = this;
                    api.getPicture({
                        sourceType: 'album',
                        encodingType: 'jpg',
                        mediaValue: 'pic',
                        destinationType: 'url',
                        allowEdit: true,
                        quality: 50,
                        targetWidth: 750,
                        targetHeight: 750,
                        saveToPhotoAlbum: false
                    }, function(ret, err) {
                        if (ret != undefined) {
                            if (ret.data != undefined || ret.data != '') {
                                var filename = Math.random() + ret.data.substr(ret.data.length - 4, 4)
                                var path = ret.data
                                    // 压缩
                                var imageFilter = api.require('imageFilter');
                                imageFilter.compress({
                                    img: path,
                                    quality: 0.5,
                                    save: {
                                        album: false,
                                        imgPath: api.cacheDir + '/upload/',
                                        imgName: filename
                                    }
                                }, function(ret, err) {
                                    if (ret.status) {
                                        $app.ajax(
                                            service['uploadHeadImage'], {
                                                data: {
                                                    values: {
                                                        'openid': that.openId,
                                                        'user_id': $api.getStorage("user").data.id,
                                                        'unionid': $api.getStorage("user").data.shop_unionid,
                                                        'app_unionid': $api.getStorage("user").data.unionid,
                                                        "mask": "上传头像中..."
                                                    },
                                                    files: {
                                                        "file": api.cacheDir + '/upload/' + filename
                                                    }
                                                },
                                                method: 'post',
                                                showMask: true
                                            },
                                            function(res) {
                                                $app.toast('上传成功');
                                                api.sendEvent({
                                                    name: 'set__',
                                                    extra: {}
                                                });
                                                api.sendEvent({
                                                    name: 'login_flag',
                                                    extra: {}
                                                });
                                                // api.sendEvent({
                                                //     name: 'edit',
                                                //     extra: {
                                                //         openid: $api.getStorage("user").data.openid
                                                //     }
                                                // });
                                                // $api.setTimeout(function() {
                                                //     that.init()
                                                // }, 500)
                                            }
                                        )
                                    }

                                });
                            }
                        }
                    })
                },
                // 检测版本
                checkAppVersion: function() {
                    var that = this
                    $app.checkAppVersion(dialog, 'U_up')
                },

                // 更新版本
                setAppVersion: function() {
                    api.ajax({
                        url: service["setAppVersion"],
                        method: 'get',
                        data: {
                            values: {
                                "app_ver": $app.appVersion,
                                "mask": "none",
                                "version": $app.appVersion
                            }
                        }
                    }, function(res, err) {
                        alert(JSON.stringify(res))
                    });

                },
                // 修改昵称
                goEdit: function() {
                    var that = this;
                    dialog.prompt({
                        title: "修改昵称",
                        text: "请输入昵称",
                        buttons: ['取消', '确定']
                    }, function(ret) {
                        // alert(JSON.stringify(ret));
                        if (ret.buttonIndex == 2) { //  确认
                            var name = $api.trim(ret.text);

                            if (name == '' || name == null) {
                                api.toast({
                                    msg: "请输入正确的昵称...",
                                    duration: 2000,
                                    location: 'middle'
                                });
                                return;
                            } else if (name.length > 10) {
                                api.toast({
                                    msg: "昵称字数超过上限...",
                                    duration: 2000,
                                    location: 'middle'
                                });
                                return;
                            }
                            //--判断是否存在特殊字符
                            if ($app.hasSpecialChar(name, '请正确输入昵称...', '')) {
                                return;
                            }

                            $app.ajax(
                                service['editNickName'], {
                                    data: {
                                        values: {
                                            "nickname": name
                                        }
                                    },
                                    method: 'post',
                                    showMask: true
                                },
                                function(res) {
                                    // alert(JSON.stringify(res))
                                    $app.toast('修改昵称成功...');
                                    api.sendEvent({
                                        name: 'login_flag',
                                        extra: {}
                                    });


                                })
                        }
                    })
                },
                // 绑定手机号
                mob_bd: function() {
                    api.openWin({
                        name: 'mob',
                        url: '../../../html/set/mob_bd.html',
                        pageParam: {}
                    });
                },
                // 微信授权
                wx_login: function() {
                    if (true) {
                        var that = this;
                        weiXin.auth({}, function(ret, err) {
                            if (ret.status) {
                                // 获取到token  ret.token
                                // 获取授权用户openid
                                weiXin.getToken({
                                    code: ret.code
                                }, function(ret, err) {
                                    if (ret.status) {
                                        weiXin.getUserInfo({
                                            accessToken: ret.accessToken,
                                            openId: ret.openId
                                        }, function(ret, err) {
                                            if (ret.status) {
                                                $api.setStorage('real_openid', ret.openid);
                                                $app.wx_bind(ret.unionid, that.openId, function(res) {
                                                    api.toast({
                                                        msg: '绑定成功',
                                                        duration: 2000,
                                                        location: 'middle'
                                                    });
                                                    $app.getUserInfo($api.getStorage('user').data.openid, function(res) {
                                                        that.init()
                                                    })
                                                })
                                            } else {
                                                api.toast({
                                                    msg: 'user绑定失败' + err.code,
                                                    duration: 2000,
                                                    location: 'middle'
                                                });
                                            }
                                        });

                                    } else {
                                        api.toast({
                                            msg: 'token绑定失败' + err.code,
                                            duration: 2000,
                                            location: 'middle'
                                        });
                                    }
                                });

                            } else {
                                api.toast({
                                    msg: 'auth绑定失败' + err.code,
                                    duration: 2000,
                                    location: 'middle'
                                });
                            }
                        })
                    }
                },
                // 修改密码
                goMI: function() {
                    api.openWin({
                        name: 'pass',
                        url: '../../../html/set/pass.html',
                        pageParam: {}
                    });
                },
                // 关于我们
                widgetToAbout: function() {
                    api.openWin({
                        name: 'about',
                        url: '../../../html/set/about.html',
                        pageParam: {}
                    });
                },
                // 下载app
                U_up: function() { //安卓版本
                    api.download({
                        url: app.android_URL,
                        savePath: '',
                        report: true,
                        allowResume: true
                    }, function(ret, err) {
                        if (ret.state == 1) {
                            //下载成功
                            $api.css($api.dom("#V_box"), 'display:none')
                            api.installApp({
                                appUri: ret.savePath
                            });
                        } else if (ret.state == 0) { //下载中
                            $api.css($api.dom("#V_box"), 'display:block')
                            var width = ret.percent + "%"
                            $api.text($api.dom("#span_"), width)
                            $api.css($api.dom("#V_win_con_title_"), 'width:' + width)
                        }
                    });
                },
                // 开启关闭通知
                Tz: function() {
                    var that = this
                    if (this.isPushStopped_num == 0) { //已开启
                        ajpush.stopPush(function(ret) {
                            that.init()
                        });
                    } else if (this.isPushStopped_num == 1) { //未开启
                        ajpush.resumePush(function(ret) {
                            that.init()
                        });
                    }
                },
                // 退出登录
                over: function() {
                    $api.clearStorage();
                    // api.setPrefs({
                    // //测试用
                    //     key: 'isNeedWelcome',
                    //     value: ''
                    // });
                    api.closeFrameGroup({
                        name: 'shop_index'
                    });
                    $app.is_login()
                }
            }
        }
        var even = function() {}
        var newVue = new Vue(vm);
        even();
    })
}
